% %% 这是WOD剖面数据生成的function
% clc;
% 
% % 
% % file = 'H:\ftp\Argo\WOD剖面\ocldb1705758023.4079.CTD\ocldb1705758023.4079.CTD.nc';
% % cast = ncread(file,'cast');
% % ncdisp(file)
% 
s = shaperead('H:\code\大论文数据处理\所有南海代码重构\shp\1000deep_line.shp');
X = s.X(1:end - 1);
Y = s.Y(1:end - 1);
%%%%%% 由于使用的闭合曲线点太多，重采样至200个点
n = 200;
tq = 0:1/n:1;
xyq = interpclosed(X,Y,tq);
X = xyq(1,:);
Y = xyq(2,:);
%%%%%

extent = [Y ;X];
% Extent为2*N 表示一组按照顺序闭合排列的点
% 为了简便，当使用矩形范围时也可以用[y1 y2;x1 x2]表示

% 两种初始化方法

% 1.通过构造函数构造一个空ArgoTempSaltProfileClass，然后通过setExtent设置范围
% TSprofile = ArgoTempSaltProfileClass();
% TSprofile.setExtent(extent);

% 2.直接通过构造函数构建
TSprofile = ArgoTempSaltProfileClass('extent',extent);



% 文件夹名称特征
% float_name_enum = ['XBT'; 'PFL' ;'OSD' ;'MRB'; 'MBT' ;'GLD'; 'DRB' ;'CTD'];
float_name_enum = [ 'PFL' ;'OSD' ;'GLD'; 'CTD'];

for i = 1:length(float_name_enum)
    % 文件夹前缀
    const_prefix = 'ocldb1705991241.14021.';
    % 导入文件夹特征作为后缀
    profileType = float_name_enum(i,:);
    const_name = [const_prefix float_name_enum(i,:)];
    float_path_name = ['H:\code\大论文数据处理\南海WOD数据处理\WOD\' const_name '\'];
    filelist = dir(float_path_name);
    filelist = filelist(4:end);
    error_count = 0;
    for j = 1:length(filelist)
        i
        j
        file = [filelist(j).folder '\' filelist(j).name];
        % ncdisp(file);
        try
            lon = ncread(file,'lon');
            lat = ncread(file,'lat');
            pres = ncread(file,'Pressure');
            % z = ncread(file,'z');
            Temperature = ncread(file,'Temperature');
            Salinity = ncread(file,'Salinity');

            Temperature_WODprofileflag  = ncread(file, 'Temperature_WODprofileflag');
            Salinity_WODprofileflag  = ncread(file, 'Temperature_WODprofileflag');
            % if Temperature_WODprofileflag~=0||Salinity_WODprofileflag~=0
            %     continue;
            % end

            Temperature_WODflag  = ncread(file, 'Temperature_WODflag');
            Salinity_WODflag  = ncread(file, 'Salinity_WODflag');
            

            Temperature_WODflag_idx = find(Temperature_WODflag~=0);
            Salinity_WODflag_idx = find(Salinity_WODflag~=0);
            Union_idx = union(Temperature_WODflag_idx, Salinity_WODflag_idx);
            if ~isempty(Union_idx)
                % disp(['i: ' num2str(i) ' j: ' num2str(j) ' 出现奇异值']);
                error_count = error_count + 1;
                Temperature(Union_idx) = nan;
                Salinity(Union_idx) = nan;
            end

            dateint = ncread(file,'date');
            if mod(dateint,100)==0
                dateint = dateint + 15;
            end
            date = datetime(num2str(dateint),'InputFormat','yyyyMMdd'); 
            TSprofile.append(lon, lat, 'temp', Temperature, Temperature_WODflag,'sail', Salinity, Salinity_WODflag, 'pres', pres, 'profileType',profileType,'date',date);
        catch
            continue;
        end
    end

end

save('WODTSprofile.mat','TSprofile');

% 
% f = figure(1);
% s = m_scatter(All_lon, All_lat,'filled');
% s.SizeData = 1;
% set(gcf,'position',[0 0 1440 780]);%设置图形窗口位置和大小
% m_proj('Mercator','lon',[100 120],'lat',[0 25])
% m_gshhs_i('patch',[.6 .6 .6]);
% m_grid;